﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TDS.Mail;

using CE.ContactUs;

public partial class contact_submit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now); 

            Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
            hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();

            divAppForm.Visible = true;
            divMessage.Visible = false;

            if (Request.Params["say"] != null)
            {
                if (Request.Params["say"].ToString() == "thankyou")
                {
                    divAppForm.Visible = false;
                    divMessage.Visible = true;
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "Your message was sent successfully. Thank you very much and we look forward to contacting you soon.";
                    //Page.RegisterStartupScript("JS_CompletedJobAppAlert", "alert('Thank you very much.\nOnline form was sent successfully.\nWe will reply back to you soon.');");
                    ClientScript.RegisterStartupScript(this.GetType(), "JS_CompletedFormAlert", "alert('" + lblMessage.Text + "');", true);
                }
            }

        }
    }


    protected void btnSubmitForm_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                if (txtSecurityCheck.Text.Trim().ToUpper() == hidSecurityCheck.Value.ToUpper())
                {
                    Int64 result = -1;
                    ContactUs _contactus = new ContactUs();
                    ContactUsDB _contactusdb = new ContactUsDB();
                    _contactus.Salutation = ddlTitle.SelectedItem.Text;
                    _contactus.Name = txtFullName.Text;
                    _contactus.Designation = txtDesignation.Text;
                    _contactus.CompanyName = txtCompanyName.Text;
                    _contactus.Tel = txtContactNumber.Text;
                    _contactus.Email = txtEmailAddress.Text;
                    _contactus.TypeofService = ddlTypeOfService.SelectedItem.Text;
                    _contactus.PreferredCountry = ddlCountry.SelectedValue;
                    _contactus.Industry = txtIndustryName.Text;
                    _contactus.Comments = txtComments.Text;
                    result = _contactusdb.Insert(_contactus);

                    if (result > 0)
                    {

                        //System.Threading.Thread.Sleep(5000);
                        MailClient _remail = new MailClient();
                        string _toEmail = System.Configuration.ConfigurationSettings.AppSettings["TDS.ContactUsEmailReceiver"].ToString();
                        string _bccEmail = string.Empty;
                        string _strTitle = "Online Contact Form (Full Name: " + txtFullName.Text + ") at Date/Time:" + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");

                        System.Text.StringBuilder _strBody = new System.Text.StringBuilder();
                        _strBody.Append("Dear Sir/Madam,<br/><br/>");
                        _strBody.Append("Please check following details.<br/><br/>");
                        _strBody.Append("<table style=\"width:80%;\" cellspacing=\"5\" cellpadding=\"5\">");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Full Name</td><td valign=\"top\">:</td><td>" + ddlTitle.SelectedItem.Text + " " + Server.HtmlEncode(txtFullName.Text) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Designation</td><td valign=\"top\">:</td><td>" + Server.HtmlEncode(txtDesignation.Text) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Contact Number</td><td valign=\"top\">:</td><td>" + Server.HtmlEncode(txtContactNumber.Text.Trim()) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\">Email Address</td><td>:</td><td>" + txtEmailAddress.Text + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\">Company</td><td>:</td><td>" + Server.HtmlEncode(txtCompanyName.Text) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\">Industry</td><td>:</td><td>" + Server.HtmlEncode(txtIndustryName.Text) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Type of service required</td><td valign=\"top\">:</td><td>" + ddlTypeOfService.SelectedItem.Text + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Preferred Country</td><td valign=\"top\">:</td><td>" + ddlCountry.SelectedItem.Text + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Comments</td><td valign=\"top\">:</td><td>" + Server.HtmlEncode(txtComments.Text.Trim()).Replace("\r\n", "<br/>") + "</td></tr>");
                        _strBody.Append("</table>");

                        if (_remail.SendNetNoreplyMail(_strTitle, _strBody.ToString(), "Teledirect Contact Us", "Teledirect", _toEmail, string.Empty, _bccEmail))
                        {
                            MailClient _autoreplyemail = new MailClient();
                            _toEmail = txtEmailAddress.Text;
                            _bccEmail = string.Empty;
                            _strTitle = "Teledirect Contact Us";

                            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());
                            string domainUrl = "";
                            string ConfirmPath = "";
                            string userType = "";
                            //domainUrl = "http://" + myUri.Host;
                            domainUrl = "http://" + myUri.Host + ":" + myUri.Port;

                            string confirmUrl = domainUrl;
                            string imgUrl = domainUrl;
                            confirmUrl += ConfirmPath;

                            imgUrl += CE.Configuration.MyConfigurationManager.GetAppSettingValue("ContactUsHeaderPath");

                            _strBody = new System.Text.StringBuilder();
                            _strBody.Append("<table cellpadding='0' cellspacing='0' width='650' align='center' style='background:white;'><tr><td valign='top' align='center'>");
                            _strBody.Append("<a href='http://www.teledirect.com.sg/' target='_blank'><img src='" + imgUrl + "' width='650' height='179' alt='Thank you for your interest!'></a>");
                            _strBody.Append("</td><tr><td>");
                            _strBody.Append("<table cellpadding='0' cellspacing='0' width='650' align='center' style='border:#999 1px solid;'><tr><td valign='top'>");
                            _strBody.Append("<table cellpadding='30' cellspacing='0' width='100%' style='font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#333;'><tr><td>");
                            _strBody.Append("With 5 contact centers strategically located in Singapore, Hong Kong, Kuala Lumpur, Bangkok and Jakarta, Teledirect can help you rapidly deploy qualified professionals in secure and high availability environments.");
                            _strBody.Append("<br><br>");
                            _strBody.Append("Our sales teams will contact you soon to discuss your requirements.");
                            _strBody.Append("<br><br><br><br>");
                            _strBody.Append("<em style='color:#06C;'>");
                            _strBody.Append("Employee testimonial");
                            _strBody.Append("<br><br>");
                            _strBody.Append("I started 12 years ago as a call-centre agent at Teledirect and I now manage a team of 300 fun people.  What a ride!");
                            _strBody.Append("<br><br>");
                            _strBody.Append("Nizam");
                            _strBody.Append("</em>");
                            _strBody.Append("</div></td></tr></table>");
                            _strBody.Append("</td></tr></table>");
                            _strBody.Append("</td></tr></table>");
                            _strBody.Append("<br>");
                            _strBody.Append("<table cellpadding='0' cellspacing='0' align='center' width='650' style='font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#999;'><tr><td align='center'>");
                            _strBody.Append("Email us: contactus@teledirect.com.sg<br/>");
                            _strBody.Append("Website: www.teledirect.com.sg");
                            _strBody.Append("</td></tr></table>");

                            if (_autoreplyemail.SendNetNoreplyMail(_strTitle, _strBody.ToString(), "Teledirect Contact Us", "Teledirect", _toEmail, string.Empty, _bccEmail))
                            {
                                Response.Redirect("submit.aspx?say=thankyou");
                            }

                        }
                        else
                        {
                            divMessage.Visible = true;
                            lblMessage.Text = "Error found! Failed to send message.<br/>Pleas try again it later.";
                            lblMessage.Font.Bold = true;
                            lblMessage.ForeColor = System.Drawing.Color.Red;
                        }
                    }

                }
                else
                {
                    divMessage.Visible = true;
                    lblMessage.Text = "Wrong security check code. Please try again!";
                    lblMessage.Font.Bold = true;
                    lblMessage.ForeColor = System.Drawing.Color.Red;

                    Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
                    hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();
                    //lblMessage.Text = "Encrypt:" + hidSecurityCheck.Value.ToString() + ", Decrypt:" + MOC.Utilities.Security.MySecurity.Decrypt(hidSecurityCheck.Value.ToString(), true);
                }

            }
            catch (Exception Ex)
            {
                divMessage.Visible = true;
                lblMessage.Text = "Error found! Please try it later again.<br/>" + Ex.Message;
                lblMessage.Font.Bold = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }



}
